package com.situ.fly.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.situ.fly.entity.User;

/*
 * 1. 创建好Mapper接口, 表名+Mapper
 * 2. 在接口前, 添加一个注解@Mapper
 * 		将当前的接口作为MyBatis的访问接口, 创建对象放到Spring容器中
 * 		Mybatis底层, 使用动态代理的方式, 为接口创建了代理对象
 */
@Mapper
public interface UserMapper {

	
	/*
	 * 配置Mapper层的方法, 根据将执行的SQL类型决定 
	 * @Insert("")
	 * @Delete("")
	 * @Update("")
	 * @Select("")
	 * 
	 */
	/**
	 * 插入用户信息
	 * @param user
	 * @return
	 */
	@Insert("insert into user (username, password, nickname)"
			+ " value(#{username}, #{password}, #{nickname})")
	int insert(User user);
	
	/**
	 * 根据用户名查询用户
	 * @param username
	 * @return
	 */
	@Select("select * from user where username=#{username}")
	User selectByUserName(String username);
	/**
	 * 根据昵称查询用户
	 * @param nickname
	 * @return
	 */
	@Select("select * from user where nickname=#{nickname}")
	User selectByNickName(String nickname);

	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@Update("update user set phone=#{phone}, email=#{email}, "
			+ "nickname=#{nickname}, sex=#{sex}, city=#{city},"
			+ "signature=#{signature}, kiss=#{kiss} where id=#{id}")
	int update(User user);

	/**
	 * 根据id查询用户信息
	 * @param id
	 * @return
	 */
	@Select("select * from user where id=#{id}")
	User selectById(Integer id);

	/**
	 * 修改密码
	 * @param id
	 * @param password
	 * @return
	 */
	@Update("update user set password=#{password} where id=#{id}")
	int updatePassword(Integer id, String password);

	/**
	 * 修改用户头像
	 * @param id
	 * @param head
	 * @return
	 */
	@Update("update user set head=#{head} where id=#{id}")
	int updateHead(Integer id, String head);

	/**
	 * 根据回复数量获取用户信息, 只取前limit条
	 * @param limit
	 * @return
	 */
	@Select("select `user`.*, count(answer.id) replynum from user " + 
			"left join answer on `user`.id=answer.uid " + 
			"group by `user`.id " + 
			"order by count(answer.id) desc " + 
			"limit #{limit}")
	List<Map<String, Object>> selectByReplyNum(Integer limit);

}





